package com.sskj.one.proxy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.mvchelper.refresh.MVCSmartHelper;
import com.sskj.lib.util.HideUtil;
import com.sskj.one.R;
import com.sskj.one.bean.AssetListBean;
import com.sskj.one.bean.GonggaoBean;
import com.sskj.one.bean.LevelBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.Observable;

/**
 * @author Hey
 * Create at  2019/05/22
 */
public class DetailTeamActivity extends BaseActivity<DetailTeamPresenter> {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshView)
    SmartRefreshLayout smartRefreshView;

    private BaseAdapter<LevelBean> adapter;

    private int page = 1;
    private int size = 10;

    private String level;

    @Override
    public int getLayoutId() {
        return R.layout.app_activity_detail_team;
    }

    @Override
    public DetailTeamPresenter getPresenter() {
        return new DetailTeamPresenter();
    }

    @Override
    public void initView() {
        level = getIntent().getStringExtra("level");
        tvTitle.setText(int2chineseNum(Integer.valueOf(level)) + "级团队详情");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<LevelBean>(R.layout.item_team_detail, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, LevelBean item) {
                ImageView iv_head = holder.getView(R.id.iv_head);
                ImageUtil.setCircleImage(item.getUpic(), iv_head);
                holder.setText(R.id.tv_mobile, CheckUtil.isMobile(item.getMobile()) ? HideUtil.getPhoneHide(item.getMobile()) : item.getMobile())
                        .setText(R.id.tv_date, item.getReg_time())
                        .setText(R.id.tv_name, item.getNickname());
            }
        };
    }

    @Override
    public void initData() {
        mPresenter.getLevelList(true, page, size, level);
        smartRefreshView.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getLevelList(false, page, size, level);
        });
        smartRefreshView.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getLevelList(false, page, size, level);
        });

    }

    public static void start(Context context, String level) {
        Intent intent = new Intent(context, DetailTeamActivity.class);
        intent.putExtra("level", level);
        context.startActivity(intent);
    }

    public static String int2chineseNum(int src) {
        final String num[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        final String unit[] = {"", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String dst = "";
        int count = 0;
        while (src > 0) {
            dst = (num[src % 10] + unit[count]) + dst;
            src = src / 10;
            count++;
        }
        return dst.replaceAll("零[千百十]", "零").replaceAll("零+万", "万")
                .replaceAll("零+亿", "亿").replaceAll("亿万", "亿零")
                .replaceAll("零+", "零").replaceAll("零$", "");
    }

    public void getLevelOK(List<LevelBean> data) {
        if (data == null) {
            return;
        }
        if (page == 1) {
            adapter.setNewData(data);
        } else {
            adapter.addData(data);
        }
    }

    public void stopRefresh() {
        smartRefreshView.finishRefresh();
        smartRefreshView.finishLoadMore();
    }


}
